package com.app.megaprosoft.modyimcarcare;

import java.util.ArrayList;

import com.app.megaprosoft.DB.Item;
import com.app.megaprosoft.DB.ItemGroup;
import com.app.megaprosoft.DB.ItemGroupMain;
import com.app.megaprosoft.DB.ItemPrice;
import com.app.megaprosoft.DB.SQLiteDBClass;
import com.app.megaprosoft.DB.ServicePOS;
import com.app.megaprosoft.general.DateTime;
import com.app.megaprosoft.general.Number;
import com.megaprosoft.modyimcarcare.R;

import android.R.integer;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;


public class Carcare_Service extends Activity {
									
	SQLiteDBClass 					myDb;
	
	private int						intDuration,
									intQty;
	private Double 					dbTotalPriceAll,
									dbPriceAll,
									dbDiscountAll,
									dbTotalPrice,
									dbDiscount,
									dbPrice;
	private String 					strarrDataItemGroupMain[][],
									strarrDataItemGroup[][],
									strarrDataItem[][],
									strarrListPOS[][],
									strarrListItem[][],
									percent="0";
	private TextView 				tv_servicedbPriceAll,
									tv_servicedbTotalPriceAll,
									tv_servicedbDiscount,
									tv_carsize,
									tv_cargroup,
									tvdbDiscount,
									_tv_sum,
									_tv_pay,
									_tv_recive,
									tv_nummin,
									tv_numitem;
	private CheckBox				cb_NoCupong,
									cb_Cupong;
	private EditText				_et_card,
									_et_itemname,
									_et_itemnumber,
									_et_itemprice,
									et_ref;
	private Button 					bt_selectall,
									bt_delete,
									bt_unmark,
									bt_clear,
									bt_save,
									bt_cash,
									bt_credit,
									bt_additem,
									_bt_paymoney,
									_bt_paycredit,
									_bt_modepay,
									_bt_fitpay,
									_bt_keypad,
									_bt_1,
									_bt_2,
									_bt_3,
									_bt_4,
									_bt_5,
									_bt_6,
									_bt_7,
									_bt_8,
									_bt_9,
									_bt_0,
									_bt_00,
									_bt_000,
									_bt_del,
									_bt_clear,
									_bt_swap,
									_bt_print,
									_bt_claerbank,
									_bt_back,
									_bt_percent,
									_bt_bath,
									_bt_other
									;
	private ImageView				_b1000,
									_b500,
									_b100,
									_b50,
									_b20,
									_b10,
									_b5,
									_b1;
	private GridView 				GV_ItemGroupMain;
	private ListView 				LV_ItemGroup,
									Carcare_PaysListView;
	
	// Itemgroupmain
	private String 					ItemGroupMainOID,
									ItemGroupMainCode,
									ItemGroupMainDesc;
	// Itemgroup
	private String 					ItemGroupOID,
									ItemGroupCode,
									ItemGroupDesc;
	// ItemPrice
	private String 					ItemPriceOID,
									CarTypeOID,
									CarSizeOID,
									CarGroupOID,
									ItemOID,
									ItemPrice,
									CarGroupName,
									CarSizeName,
									CarTypeName,
									ItemCode,
									ItemName;
	//ItemStock					
	private String 					Duration;
	//Inten
	private String 					FFUserOID,
									ArCode,
									CarSize,
									POSDocOID,
									POSList,
									ListQty,
									
									ListPrice,
									ListTotal,
									ListDiscount,
									ServicedbDiscount="0",
									Service_total="0",
									Service_ReceiveMoney="0",
									Service_NetTotalAmount="0",
									DEFINECARSIZE,
									DEFINECARGROUP;
	
	private CarCare_Adapter_ListPay carcare_pay_adapter;
	
	private String 					ServiceID	="105201310070001";
	private Boolean 				ServiceMode = false,
									Mode 		= true,// true = เพิ่ม, false = ลด
									Cupong		= false;
	private AlertDialog 			PayDialog,
									DiscountDialog,
									AdditemDialog;
	
	//Function
	ServicePOS 		servicepos 		= new ServicePOS();
	Item 			item 			= new Item();
	ItemGroup 		itemgroup 		= new ItemGroup();
	ItemGroupMain 	itemgroupmain 	= new ItemGroupMain();
	ItemPrice 		itemprice 		= new ItemPrice();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.a_carcare_activity_service);
		
		servicepos.setContext(getApplicationContext());
		item.setContext(getApplicationContext());
		itemgroup.setContext(getApplicationContext());
		itemgroupmain.setContext(getApplicationContext());
		itemprice.setContext(getApplicationContext());
		
		myDb = new SQLiteDBClass(this);
		//textview
		tv_carsize = (TextView) findViewById(R.id.tv_carcare_carsize);
		tv_cargroup = (TextView) findViewById(R.id.tv_carcare_cargroup);
		tvdbDiscount= (TextView) findViewById(R.id.text_discount);
		tv_numitem = (TextView) findViewById(R.id.tv_carcare_numitem);
		tv_nummin = (TextView) findViewById(R.id.tv_carcare_nummin);
		
		tv_servicedbPriceAll = (TextView) findViewById(R.id.tv_service_priceall);
		tv_servicedbTotalPriceAll = (TextView) findViewById(R.id.tv_service_totalpriceall);
		tv_servicedbDiscount = (TextView) findViewById(R.id.tv_service_discount);
		//button 
		bt_selectall = (Button) findViewById(R.id.a_service_bt_selectall);
		bt_selectall.setOnClickListener(ButtonCheckall);
		bt_delete = (Button) findViewById(R.id.a_service_bt_delete);
		bt_delete.setOnClickListener(Buttondelete);
		bt_clear = (Button) findViewById(R.id.a_service_bt_clear);
		bt_clear.setOnClickListener(ButtonClear);
		bt_unmark = (Button) findViewById(R.id.a_service_bt_unmark);
		bt_unmark.setOnClickListener(ButtonUnmark);
		bt_cash = (Button) findViewById(R.id.a_service_cash);
		bt_cash.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				PayDialog.show();
			}
		});
		bt_credit = (Button) findViewById(R.id.a_service_discount);
		bt_credit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				DiscountDialog.show();
			}
		});
		bt_additem = (Button) findViewById(R.id.a_service_additem);
		bt_additem.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				AdditemDialog.show();
			}
		});
		bt_save = (Button) findViewById(R.id.a_service_save);
		bt_save.setOnClickListener(ButtonSave);
		//View
		GV_ItemGroupMain  = (GridView)findViewById(R.id.carcare_service_itemgroupmain);
		LV_ItemGroup = (ListView) findViewById(R.id.carcare_service_itemgroup);
		Carcare_PaysListView = (ListView) findViewById(R.id.carcare_service_item);
		
		Bundle bundle = getIntent().getExtras();
		if (bundle != null) {
			setFFUserOID((String) bundle.get("FFUserOID"));
			setArCode((String) bundle.get("ArCode"));
			setCarSizeOID((String) bundle.get("CarSize"));
			setDEFINECARSIZE((String) bundle.get("CarSize"));
			tv_carsize.setText(getCarSizeOID());
			setCarGroupOID((String) bundle.get("CarGroupOID"));
			setDEFINECARGROUP((String) bundle.get("CarGroupOID"));
			tv_cargroup.setText((String) bundle.get("CarGroup"));
			setPOSDocOID((String) bundle.get("POSDocOID"));
		}
		
		LayoutInflater inflater = Carcare_Service.this.getLayoutInflater();
		
		AdditemDialog = new AlertDialog.Builder(Carcare_Service.this)
        .setView(inflater.inflate(R.layout.a_carcare_dialog_additem, null))
        .setTitle("เพิ่มบริการ/สินค้า")
        .setIcon(R.drawable.addbutton1)
        .setCancelable(true)
        .create();
		
		PayDialog = new AlertDialog.Builder(Carcare_Service.this)
        .setView(inflater.inflate(R.layout.a_carcare_dialog_pay, null))
        .setTitle("ชำระเงิน")
        .setIcon(R.drawable.money)
        .setCancelable(true)
        .create();
		
		DiscountDialog = new AlertDialog.Builder(Carcare_Service.this)
        .setView(inflater.inflate(R.layout.a_carcare_dialog_discount, null))
        .setTitle("ส่วนลด")
        .setIcon(R.drawable.money2)
        .setCancelable(true)
        .create();
		
		AdditemDialog.setOnShowListener(new DialogInterface.OnShowListener() {
			
			@Override
			public void onShow(DialogInterface dialog) {
				Mode=true;
				final InputMethodManager imm = (InputMethodManager)getSystemService(
					      Context.INPUT_METHOD_SERVICE);
				_bt_print 		=	(Button) AdditemDialog.findViewById(R.id.submit);
				_bt_print.setOnClickListener(SUBMITitem);
				
				_et_itemname		=  	(EditText) AdditemDialog.findViewById(R.id.itemname);
				_et_itemnumber 		=  	(EditText) AdditemDialog.findViewById(R.id.itemnumber);
				_et_itemprice 		=  	(EditText) AdditemDialog.findViewById(R.id.itemprice);
				_et_itemname.setText("");
				_et_itemnumber.setText("");
				_et_itemprice.setText("");
				_bt_back		=	(Button) AdditemDialog.findViewById(R.id.back);
				_bt_back.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
						AdditemDialog.dismiss();
						imm.hideSoftInputFromWindow(_et_itemname.getWindowToken(), 0);
						imm.hideSoftInputFromWindow(_et_itemnumber.getWindowToken(), 0);
						imm.hideSoftInputFromWindow(_et_itemprice.getWindowToken(), 0);
						getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
					}                                                                                                                                                                                                           
				});
			}
		});

		DiscountDialog.setOnShowListener(new DialogInterface.OnShowListener() {
			
			@Override
			public void onShow(DialogInterface dialog) {
				Mode	= false;
				Cupong 	= false;
				final InputMethodManager imm = (InputMethodManager)getSystemService(
					      Context.INPUT_METHOD_SERVICE);
				final LinearLayout ref = (LinearLayout) DiscountDialog.findViewById(R.id.cupong); 
				cb_NoCupong 	= 	(CheckBox) DiscountDialog.findViewById(R.id.checkBoxNoCupong);
				cb_Cupong 		 = 	(CheckBox) DiscountDialog.findViewById(R.id.checkBoxCupong);
				cb_NoCupong.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						Cupong = false;
						ref.setVisibility(View.GONE);
						cb_Cupong.setChecked(false);
						cb_NoCupong.setChecked(true);
						et_ref.setText("");
						imm.hideSoftInputFromWindow(et_ref.getWindowToken(), 0);
						getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
					}
				});
				cb_Cupong.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						Cupong = true;
						ref.setVisibility(View.VISIBLE);
						cb_Cupong.setChecked(true);
						cb_NoCupong.setChecked(false);
						
					}
				});
				
				_bt_print 		=	(Button) DiscountDialog.findViewById(R.id.submit);
				_bt_print.setOnClickListener(SUBMITDiscount);
				_bt_back		=	(Button) DiscountDialog.findViewById(R.id.back);
				_bt_back.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						DiscountDialog.dismiss();
						imm.hideSoftInputFromWindow(et_ref.getWindowToken(), 0);
						getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
					}
				});
				_tv_sum 		=  	(TextView) DiscountDialog.findViewById(R.id.sum);
				_tv_sum.setHint(getPercent());
				_tv_sum.setText("");
				et_ref			=	(EditText) DiscountDialog.findViewById(R.id.ref);
				
				_bt_percent		=	(Button) DiscountDialog.findViewById(R.id.percent);
				_bt_percent.setBackgroundColor(Color.GRAY);
				_bt_bath		=	(Button) DiscountDialog.findViewById(R.id.bath);
				_bt_bath.setBackgroundColor(Color.GREEN);
				_bt_percent.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						if(Mode==false){
							Mode = true;
							_tv_sum.setText("");
							_tv_sum.setHint(getPercent());
							_bt_percent.setBackgroundColor(Color.GREEN);
							_bt_bath.setBackgroundColor(Color.GRAY);
							ref.setVisibility(View.GONE);
							cb_Cupong.setChecked(false);
							cb_NoCupong.setChecked(true);
							et_ref.setText("");
							imm.hideSoftInputFromWindow(et_ref.getWindowToken(), 0);
							getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
						}
					}
				});
				_bt_bath.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						if(Mode==true){
							Mode = false;
							_tv_sum.setText("");
							_bt_percent.setBackgroundColor(Color.GRAY);
							_bt_bath.setBackgroundColor(Color.GREEN);
						}
					}
				});
				
				//Pad
				_bt_clear	=	(Button) DiscountDialog.findViewById(R.id.ClearPad);
				_bt_clear.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						_tv_sum.setText("");
					}
				});
				_bt_del	=	(Button) DiscountDialog.findViewById(R.id.delpad);
				_bt_del.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						CalPaddelDiscount();
					}
				});
				_bt_1	=	(Button) DiscountDialog.findViewById(R.id.pad1);
				_bt_1.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						CalPadDiscount("1");
					}
				});
				_bt_2	=	(Button) DiscountDialog.findViewById(R.id.pad2);
				_bt_2.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						CalPadDiscount("2");
					}
				});
				_bt_3	=	(Button) DiscountDialog.findViewById(R.id.pad3);
				_bt_3.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						CalPadDiscount("3");
					}
				});
				_bt_4	=	(Button) DiscountDialog.findViewById(R.id.pad4);
				_bt_4.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						CalPadDiscount("4");
					}
				});
				_bt_5	=	(Button) DiscountDialog.findViewById(R.id.pad5);
				_bt_5.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						CalPadDiscount("5");
					}
				});
				_bt_6	=	(Button) DiscountDialog.findViewById(R.id.pad6);
				_bt_6.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						CalPadDiscount("6");
					}
				});
				_bt_7	=	(Button) DiscountDialog.findViewById(R.id.pad7);
				_bt_7.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						CalPadDiscount("7");
					}
				});
				_bt_8	=	(Button) DiscountDialog.findViewById(R.id.pad8);
				_bt_8.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						CalPadDiscount("8");
					}
				});
				_bt_9	=	(Button) DiscountDialog.findViewById(R.id.pad9);
				_bt_9.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						CalPadDiscount("9");
					}
				});
				_bt_0	=	(Button) DiscountDialog.findViewById(R.id.pad0);
				_bt_0.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						CalPadDiscount("0");
					}
				});
				_bt_00	=	(Button) DiscountDialog.findViewById(R.id.pad00);
				_bt_00.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						CalPadDiscount("00");
					}
				});
				_bt_other	=	(Button) DiscountDialog.findViewById(R.id.other);
				_bt_other.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						CalPadDiscount(".");
					}
				});
				
			}
		});

		PayDialog.setOnShowListener(new DialogInterface.OnShowListener() {
			
			@Override
			public void onShow(DialogInterface dialog) {
				Mode=true;
				
				_bt_paymoney 	= 	(Button) PayDialog.findViewById(R.id.money);
				_bt_paymoney.setOnClickListener(MONEY);
				_bt_paycredit  	= 	(Button) PayDialog.findViewById(R.id.credit);
				_bt_paycredit.setOnClickListener(CREDIT);
				_bt_print 		=	(Button) PayDialog.findViewById(R.id.submit);
				_bt_print.setOnClickListener(SUBMIT);
				_bt_back		=	(Button) PayDialog.findViewById(R.id.back);
				_bt_back.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						PayDialog.dismiss();
					}
				});
				_tv_sum 		=  	(TextView) PayDialog.findViewById(R.id.sum);
				_tv_sum.setText(getService_NetTotalAmount());
				_tv_pay 		= 	(TextView) PayDialog.findViewById(R.id.pay);
				_tv_pay.setText("0.00");
				_tv_recive 		= 	(TextView) PayDialog.findViewById(R.id.recive);
				_tv_recive.setText("0.00");
				_et_card		= 	(EditText) PayDialog.findViewById(R.id.card);
				final InputMethodManager imm = (InputMethodManager)getSystemService(
					      Context.INPUT_METHOD_SERVICE);
				imm.hideSoftInputFromWindow(_et_card.getWindowToken(), 0);
				getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
				//Bank
				_b1000 			=	(ImageView) PayDialog.findViewById(R.id.b1000);
				_b1000.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						CalBank("1000");
					}
				});
				_b500 			=	(ImageView) PayDialog.findViewById(R.id.b500);
				_b500.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						CalBank("500");
					}
				});
				_b100 			=	(ImageView) PayDialog.findViewById(R.id.b100);
				_b100.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						CalBank("100");
					}
				});
				_b50 			=	(ImageView) PayDialog.findViewById(R.id.b50);
				_b50.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						CalBank("50");
					}
				});
				_b20			=	(ImageView) PayDialog.findViewById(R.id.b20);
				_b20.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						CalBank("20");
					}
				});
				_b10			=	(ImageView) PayDialog.findViewById(R.id.b10);
				_b10.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						CalBank("10");
					}
				});
				_b5				=	(ImageView) PayDialog.findViewById(R.id.b5);
				_b5.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						CalBank("5");
					}
				});
				_b1				=	(ImageView) PayDialog.findViewById(R.id.b1);
				_b1.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						CalBank("1");
					}
				});
				_bt_modepay		=	(Button) PayDialog.findViewById(R.id.mode);
				_bt_modepay.setBackgroundColor(Color.GREEN);
				_bt_modepay.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						if(Mode==true){
							Mode = false;
							_bt_modepay.setText("+");
							_bt_modepay.setBackgroundColor(Color.RED);
						}else{
							Mode = true;
							_bt_modepay.setText("-");
							_bt_modepay.setBackgroundColor(Color.GREEN);
						}
					}
				});
				_bt_claerbank	=	(Button) PayDialog.findViewById(R.id.clearbank);
				_bt_claerbank.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						Mode = true;
						_bt_modepay.setText("-");
						_bt_modepay.setBackgroundColor(Color.GREEN);
						_tv_pay.setText("0.00");
			        	_tv_recive.setText("0.00");
					}
				});
				_bt_fitpay		=	(Button) PayDialog.findViewById(R.id.fit);
				_bt_fitpay.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						_tv_pay.setText("0.00");
			        	_tv_recive.setText("0.00");
						CalBank(getService_NetTotalAmount());
					}
				});
				_bt_keypad		=	(Button) PayDialog.findViewById(R.id.keypad);
				_bt_keypad.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						LinearLayout lnkeypad = (LinearLayout) PayDialog.findViewById(R.id.tabkeybord);
						LinearLayout lnmoney = (LinearLayout) PayDialog.findViewById(R.id.tabmoney);
						lnmoney.setVisibility(View.GONE);
			        	lnkeypad.setVisibility(View.VISIBLE);
			        	_tv_pay.setText("0");
			        	_tv_recive.setText("0");
					}
				});
				//Pad
				_bt_swap	=	(Button) PayDialog.findViewById(R.id.swappad);
				_bt_swap.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						LinearLayout lnkeypad = (LinearLayout) PayDialog.findViewById(R.id.tabkeybord);
						LinearLayout lnmoney = (LinearLayout) PayDialog.findViewById(R.id.tabmoney);
						lnmoney.setVisibility(View.VISIBLE);
			        	lnkeypad.setVisibility(View.GONE);
			        	_tv_pay.setText("0.00");
			        	_tv_recive.setText("0.00");
					}
				});
				_bt_clear	=	(Button) PayDialog.findViewById(R.id.ClearPad);
				_bt_clear.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						_tv_pay.setText("0");
			        	_tv_recive.setText("0.00");
					}
				});
				_bt_del	=	(Button) PayDialog.findViewById(R.id.delpad);
				_bt_del.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						CalPaddel();
					}
				});
				_bt_1	=	(Button) PayDialog.findViewById(R.id.pad1);
				_bt_1.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						CalPad("1");
					}
				});
				_bt_2	=	(Button) PayDialog.findViewById(R.id.pad2);
				_bt_2.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						CalPad("2");
					}
				});
				_bt_3	=	(Button) PayDialog.findViewById(R.id.pad3);
				_bt_3.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						CalPad("3");
					}
				});
				_bt_4	=	(Button) PayDialog.findViewById(R.id.pad4);
				_bt_4.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						CalPad("4");
					}
				});
				_bt_5	=	(Button) PayDialog.findViewById(R.id.pad5);
				_bt_5.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						CalPad("5");
					}
				});
				_bt_6	=	(Button) PayDialog.findViewById(R.id.pad6);
				_bt_6.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						CalPad("6");
					}
				});
				_bt_7	=	(Button) PayDialog.findViewById(R.id.pad7);
				_bt_7.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						CalPad("7");
					}
				});
				_bt_8	=	(Button) PayDialog.findViewById(R.id.pad8);
				_bt_8.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						CalPad("8");
					}
				});
				_bt_9	=	(Button) PayDialog.findViewById(R.id.pad9);
				_bt_9.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						CalPad("9");
					}
				});
				_bt_0	=	(Button) PayDialog.findViewById(R.id.pad0);
				_bt_0.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						CalPad("0");
					}
				});
				_bt_00	=	(Button) PayDialog.findViewById(R.id.pad00);
				_bt_00.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						CalPad("00");
					}
				});
				_bt_000	=	(Button) PayDialog.findViewById(R.id.pad000);
				_bt_000.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						CalPad("000");
					}
				});
				LinearLayout lnmoney = (LinearLayout) PayDialog.findViewById(R.id.tabmoney);
	        	LinearLayout lncredit = (LinearLayout) PayDialog.findViewById(R.id.tabcredit);
	        	LinearLayout lnkeypad = (LinearLayout) PayDialog.findViewById(R.id.tabkeybord);
	        	lnmoney.setVisibility(View.VISIBLE);
	        	lncredit.setVisibility(View.GONE);
	        	lnkeypad.setVisibility(View.GONE);
			}
		});
		Init();
	}
	
	private void CalBank(String value){
		Double cash 	=  Double.valueOf(Number.unCommaReturnDouble(_tv_sum.getText().toString()));
		Double pay 		=   Double.valueOf(Number.unCommaReturnDouble(_tv_pay.getText().toString()));
		Double recive	=   Double.valueOf(Number.unCommaReturnDouble(_tv_recive.getText().toString()));
		Double values	=   Double.valueOf(Number.unCommaReturnDouble(value));
		if(Mode==true){
			pay = pay + values;
		}else{
			pay = pay - values;
		}
		if(pay<0){
			pay = 0.00;
		}
		if(pay>cash){
			recive = pay - cash;
		}else{
			recive = 0.00;
		}
		_tv_pay.setText(Number.addComma(pay, 2));
		_tv_recive.setText(Number.addComma(recive, 2));
	}
	
	private void CalPad(String value){
		
		int val =  Integer.valueOf(_tv_pay.getText().toString());
		if(val==0){
			_tv_pay.setText(value);
		}else{
			if(val < 1000000){
				_tv_pay.setText(String.valueOf(val)+value);
			}
		}
		val =  Integer.valueOf(_tv_pay.getText().toString());
		Double cash 	=  Double.valueOf(Number.unCommaReturnDouble(_tv_sum.getText().toString()));
		
		Double recive	=   Double.valueOf(Number.unCommaReturnDouble(_tv_recive.getText().toString()));
		
		if(val>cash){
			recive = val - cash;
		}else{
			recive = 0.00;
		}
		_tv_pay.setText(String.valueOf(val));
		_tv_recive.setText(Number.addComma(recive, 2));
	}
	
	private void CalPaddel(){
		int temp = _tv_pay.getText().length();
		if(temp<=1){
			_tv_pay.setText("0");
		}else{
			_tv_pay.setText(_tv_pay.getText().subSequence(0, temp-1));
		}
		int val =  Integer.valueOf(_tv_pay.getText().toString());
		Double cash 	=  Double.valueOf(Number.unCommaReturnDouble(_tv_sum.getText().toString()));
		
		Double recive	=   Double.valueOf(Number.unCommaReturnDouble(_tv_recive.getText().toString()));
		
		if(val>cash){
			recive = val - cash;
		}else{
			recive = 0.00;
		}
		_tv_pay.setText(String.valueOf(val));
		_tv_recive.setText(Number.addComma(recive, 2));
	}
	
	private void CalPadDiscount(String value){
		String val =  _tv_sum.getText().toString();
		
		if(val.equals("0")){
			_tv_sum.setText(value);
		}else{
			if(Mode==true){
				Double check = Number.unCommaReturnDouble(val+value);
				if(check < 101 || value.equals(".")){
					_tv_sum.setText(val+value);
				}
			}else{
				_tv_sum.setText(val+value);
			}
		}
	}
	
	private void CalPaddelDiscount(){
		int temp = _tv_sum.getText().length();
		if(temp<=1){
			_tv_sum.setHint("0");
		}else{
			_tv_sum.setText(_tv_sum.getText().subSequence(0, temp-1));
		}
	}
	
	private OnClickListener MONEY = new OnClickListener() {
        public void onClick(View v) {
        	LinearLayout lnmoney = (LinearLayout) PayDialog.findViewById(R.id.tabmoney);
        	LinearLayout lncredit = (LinearLayout) PayDialog.findViewById(R.id.tabcredit);
        	LinearLayout lnkeypad = (LinearLayout) PayDialog.findViewById(R.id.tabkeybord);
        	lnmoney.setVisibility(View.VISIBLE);
        	lncredit.setVisibility(View.GONE);
        	lnkeypad.setVisibility(View.GONE);
        }
    };
    
    private OnClickListener CREDIT = new OnClickListener() {
        public void onClick(View v) {
        	LinearLayout lnmoney = (LinearLayout) PayDialog.findViewById(R.id.tabmoney);
        	LinearLayout lncredit = (LinearLayout) PayDialog.findViewById(R.id.tabcredit);
        	LinearLayout lnkeypad = (LinearLayout) PayDialog.findViewById(R.id.tabkeybord);
        	lnmoney.setVisibility(View.GONE);
        	lncredit.setVisibility(View.VISIBLE);
        	lnkeypad.setVisibility(View.GONE);
        	_tv_pay.setText("0.00");
        	_tv_recive.setText("0.00");
			CalBank(getService_NetTotalAmount());
        }
    };
    
    private OnClickListener SUBMIT = new OnClickListener() {
        public void onClick(View v) {
        	String data[][] = null;
        	setService_ReceiveMoney(_tv_recive.getText().toString());
        	servicepos.UpdateServicePOSstatusUpdateByDoc(	
					getPOSDocOID(), 
					"0", 
					"0");
        	try {
        		data = servicepos.SelectAll_ServicePOS("", getPOSDocOID(), "");
        		int time = DateTime.convertTime(data[0][5])+Integer.valueOf(getDuration());
        		setDuration(settime(time));
        	} catch (Exception e) {
        		// TODO Auto-generated catch block
        		setDuration("");
        	}
        	servicepos.UpdatePriceAndDuration(	getPOSDocOID(), 
					getDuration(), 
					getService_total(), 
					getServicedbDiscount(), 
					getService_ReceiveMoney(), 
					getService_NetTotalAmount(), 
					"0");
        	servicepos.UpdateServicePOSStatuschancg( data[0][0], 
        			getPOSDocOID(), 
					"คืนรถ",
					DateTime.getDateTimeNow(), 
					"0",
					"1");
        	PayDialog.dismiss();
        	finish();
        }
    };
    
    private OnClickListener SUBMITitem = new OnClickListener() {
        public void onClick(View v) {
        	String number = _et_itemnumber.getText().toString();
        	String name = _et_itemname.getText().toString();
        	String price = _et_itemprice.getText().toString();
        	if(number.equals("")){
        		number="0";
        	}
        	if(price.equals("")){
        		price="0";
        	}
        	if(price.equals("")){
        		name="อื่นๆ";
        	}
        	try{
				servicepos.InsertServicePOSLists(DateTime.getDateNowymd()+DateTime.getTimeNowss(), 
						
		    			getPOSDocOID(), 
		    			"222222222222222", 
		    			number, 
		    			price, 
		    			price, 
		    			
		    			"0", 
		    			"0", 
		    			"", 
		    			"", 
		    			"", 
		    			"", 
		    			"", 
		    			"", 
		    			"", 
		    			name, 
		    			
		    			getFFUserOID(), 
		    			DateTime.getDateTimeNow(), 
		    			DateTime.getDateTimeNow(), 
		    			"0", 
		    			"0");
			}catch(Exception e){
				
			}
        	AdditemDialog.dismiss();
        	Define_ListOrder();
        }
    };
    
    private OnClickListener SUBMITDiscount = new OnClickListener() {
        public void onClick(View v) {
        	String name = "*****ส่วนลด";
        	String value = _tv_sum.getText().toString();
        	if(Mode==true){//percent
        		String discount;
        		try{
        			discount = Number.addComma(Double.valueOf(value),2);
        			servicepos.UpdateServicePOSDiscount(getPOSDocOID(),discount);
        		}catch(Exception e){
        			
        		}
        		
        	}else{//bath
        		if(Cupong==true){
        			name = "***ส่วนลดคูปอง"+value+" บาท";
        		}else{
        			name = "***ส่วนลด"+value+" บาท";
        		}
        		try{
    				servicepos.InsertServicePOSLists(DateTime.getDateNowymd()+DateTime.getTimeNowss(), 
    						
    		    			getPOSDocOID(), 
    		    			"111111111111111", 
    		    			"1", 
    		    			"0", 
    		    			"0", 
    		    			
    		    			String.valueOf(value), 
    		    			"0", 
    		    			"", 
    		    			"", 
    		    			"", 
    		    			"", 
    		    			"", 
    		    			"", 
    		    			"", 
    		    			name, 
    		    			
    		    			getFFUserOID(), 
    		    			DateTime.getDateTimeNow(), 
    		    			DateTime.getDateTimeNow(), 
    		    			"0", 
    		    			"0");
    			}catch(Exception e){
    			}
        	}
        	
        	Define_ListOrder();
        	DiscountDialog.dismiss();
        	
        }
    };
    
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		servicepos.setContext(getApplicationContext());
	}
	
	
	public void Init(){
		Define_ListOrder();
		Define_ItemGroupMain();
	}
	
	public void Define_ListOrder(){
		tv_servicedbPriceAll.setText("0");
		tv_servicedbTotalPriceAll.setText("0");
		tv_servicedbDiscount.setText("0");
		tv_numitem.setText("0");
		tv_nummin.setText("0");
		
		carcare_pay_adapter = new CarCare_Adapter_ListPay(Carcare_Service.this, 
				R.layout.list_pay, new ArrayList<CarCare_List_Payment>());
		Carcare_PaysListView.setAdapter(carcare_pay_adapter);
		
		String temp[][];
		try {
			temp = servicepos.SelectAll_ServicePOS("", getPOSDocOID(), "");
			setPercent(temp[0][15]);
			if(!getPercent().equals("0.00")){
				tvdbDiscount.setText("  *** ส่วนลด"+getPercent()+"% ***");
				tvdbDiscount.setTextColor(Color.BLUE);
			}else{
				tvdbDiscount.setText("ลูกค้าทั่วไป");
			}
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		
		dbDiscountAll=0.0;
		dbPriceAll=0.0;
		dbTotalPriceAll=0.0;
		dbPrice= 0.0;
		dbDiscount=0.0;
		dbTotalPrice=0.0;
		intDuration = 0;
		intQty= 0;
		try{
			strarrListPOS = servicepos.SelectAll_ServicePOSList(getPOSDocOID(),"0","");
//			ListPOS = myDb.SelectAll_ServicePOSList("");
			int length = strarrListPOS.length;
			tv_numitem.setText(String.valueOf(length));
			for(int i=0;i<length;i++){
				intQty = Integer.valueOf(strarrListPOS[i][3]);
				dbPrice = Double.valueOf(Number.unCommaReturnDouble(strarrListPOS[i][4]));
				dbDiscount = Double.valueOf(Number.unCommaReturnDouble(strarrListPOS[i][6]));
//				dbTotalPrice = Double.valueOf(Number.unCommaReturnDouble(ListPOS[i][5]));
				dbTotalPrice = dbPrice *intQty;
				//Duration
				
				
				int tem_dulation=0;
				int temintQty=0;
				try{
					String data[][] = itemprice.SelectAll_ItemPrice("", strarrListPOS[i][2], "", "");
					String data2[][] = item.SelectAll_Item("",data[0][4]);
//					Toast.makeText(getApplicationContext(),"itemoid : " + data[0][4], Toast.LENGTH_SHORT).show();
					tem_dulation = Integer.valueOf(data2[0][47].toString());
//					Toast.makeText(getApplicationContext(),"tem_dulation : " + data2[0][47], Toast.LENGTH_SHORT).show();
				}catch(Exception e){
//					Toast.makeText(getApplicationContext(),"tem_dulation : EE ", Toast.LENGTH_SHORT).show();
					tem_dulation= 0;
				}
				try{
					temintQty = Integer.valueOf(strarrListPOS[i][3]);
				}catch(Exception e){
					temintQty= 1;
				}
				intDuration = intDuration + (tem_dulation*temintQty);
				
				
				
				
				Boolean checked ;
				if(strarrListPOS[i][19].equals("1")){
					checked=true;
				}else{
					checked=false;
				}
				
				carcare_pay_adapter.add(new CarCare_List_Payment(checked, strarrListPOS[i][15], 
																		strarrListPOS[i][3], 
																		Number.addComma(dbPrice, 2), 
																		Number.addComma(dbDiscount, 2), 
																		Number.addComma(dbTotalPrice, 2),
																		strarrListPOS[i][2]));	
				dbDiscountAll = dbDiscountAll+dbDiscount;
				dbPriceAll = dbPriceAll+(dbPrice*intQty);
				dbTotalPriceAll = dbPriceAll-dbDiscountAll;
			}
			
			dbPriceAll = dbPriceAll - dbDiscountAll;
			if(dbPriceAll<0.00){
				dbPriceAll = 0.00;
			}
			dbDiscountAll = (Double.valueOf(Number.unCommaReturnDouble(getPercent()))*dbTotalPriceAll)/100;
			dbTotalPriceAll = (dbPriceAll)-dbDiscountAll;
			
			tv_servicedbPriceAll.setText(Number.addComma(dbPriceAll, 2));
			tv_servicedbTotalPriceAll.setText(Number.addComma(dbTotalPriceAll, 2));
			tv_servicedbDiscount.setText(Number.addComma(dbDiscountAll, 2));
			setService_total(Number.addComma(dbPriceAll, 2));
			setServicedbDiscount(Number.addComma(dbDiscountAll, 2));
			setService_ReceiveMoney("0.00");
			setService_NetTotalAmount(Number.addComma(dbTotalPriceAll, 2));
			setDuration(String.valueOf(intDuration));
			tv_nummin.setText(getDuration());
			
//			Toast.makeText(getApplicationContext(),"Duration : " + getDuration(), Toast.LENGTH_SHORT).show();
		}catch(Exception e){
		}
	}
	private Boolean Checkitem(String itemid){
		try{
			strarrListPOS = servicepos.SelectAll_ServicePOSList(getPOSDocOID(),"0","");
			int length = strarrListPOS.length;	
			for(int i=0;i<length;i++){
				if(strarrListPOS[i][2].equals(itemid)){
					setPOSList(strarrListPOS[i][0].toString());
					
					setListDiscount(strarrListPOS[i][6].toString());
					setListPrice(strarrListPOS[i][4].toString());
					setListQty(strarrListPOS[i][3].toString());
					setListTotal(strarrListPOS[i][5].toString());
					return true;
				}
			}
		}catch(Exception e){
			return false;
		}
		return false;
	}
	public void Define_ItemGroupMain(){
		try {
			strarrDataItemGroupMain = itemgroupmain.SelectAll_ItemGroupMain();
			
			GV_ItemGroupMain.setAdapter(new CarCare_Adapter_ItemGroupMain(this,strarrDataItemGroupMain));
//			strarrDataItemGroupMain.length;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
		GV_ItemGroupMain.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v,
				int position, long id) {
				setItemGroupMainOID(strarrDataItemGroupMain[position][0].toString());
				setItemGroupMainCode(strarrDataItemGroupMain[position][1].toString());
				setItemGroupMainDesc(strarrDataItemGroupMain[position][2].toString());
//				if(ServiceID.equals(getItemGroupMainCode())){
//					ServiceMode=true;
//				}else{
//					ServiceMode=false;
//				}
				Define_ItemGroup();//GroupItem
//				tv.setText(getItemGroupMainOID());
			}
		});}
	
	
	public void Define_ItemGroup(){
		strarrDataItemGroup = itemgroup.SelectAll_ItemGroup(ItemGroupMainOID);   
        LV_ItemGroup.setAdapter(new CarCare_Adapter_ItemGroup(this,strarrDataItemGroup));
        // OnClick
        LV_ItemGroup.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v,
				int position, long id) {
				try{
					setItemGroupOID(strarrDataItemGroup[position][0].toString()); 
					setItemGroupCode(strarrDataItemGroup[position][1].toString()); 
					setItemGroupDesc(strarrDataItemGroup[position][1].toString()); 
					
					if(ServiceID.equals(getItemGroupOID())){
						ServiceMode=true;
					}else{
						ServiceMode=false;
					}
//					Toast.makeText(getApplicationContext(),ServiceMode+" getItemGroupCode : " + getItemGroupOID(), Toast.LENGTH_SHORT).show();
					Define_Item();//Item
				}catch(Exception e){
				}
			}
		});
	}
	public void Define_Item(){
		if(ServiceMode){
			strarrDataItem = itemprice.SelectAll_ItemPrice(getItemGroupOID(),"",getDEFINECARSIZE(),getDEFINECARGROUP());   
		    LV_ItemGroup.setAdapter(new CarCare_Adapter_ItemPrice(this,strarrDataItem));
	        // OnClick
	        LV_ItemGroup.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {
					try{
						setItemPriceOID	(strarrDataItem[position][0].toString());
						setCarTypeOID	(strarrDataItem[position][1].toString()); 
						setCarSizeOID	(strarrDataItem[position][2].toString()); 
						setCarGroupOID	(strarrDataItem[position][3].toString()); 
						setItemOID		(strarrDataItem[position][4].toString()); 
						setItemPrice	(strarrDataItem[position][5].toString());
						
						setCarGroupName	(strarrDataItem[position][6].toString()); 
						setCarSizeName	(strarrDataItem[position][7].toString()); 
						setCarTypeName	(strarrDataItem[position][8].toString()); 
						setItemCode		(strarrDataItem[position][9].toString()); 
						setItemName		(strarrDataItem[position][10].toString()); 
//						tv_item.setText("> "+getItemName());
						if(!Checkitem(getItemPriceOID())){
							try{
								servicepos.InsertServicePOSLists(DateTime.getDateNowymd()+DateTime.getTimeNowss(), 
						    			getPOSDocOID(), getItemPriceOID(), "1", getItemPrice(), getItemPrice(), 
						    			"0", "0", "", "", "", 
						    			"", "", "", "", getItemName(), 
						    			getFFUserOID(), DateTime.getDateTimeNow(), DateTime.getDateTimeNow(), "0", "0");
							}catch(Exception e){
								
							}
						}else{
							try{
								int Qty = Integer.valueOf(getListQty())+1;
								
								double Price = Double.valueOf(Number.unCommaReturnDouble(getListPrice()));
								double Discount = Double.valueOf(Number.unCommaReturnDouble(getListDiscount()));
								double TotalAmount = Double.valueOf(Number.unCommaReturnDouble(getListTotal()));
								
								TotalAmount = (Price*Qty)-Discount;
										
								servicepos.UpdateItemServicePOSLists( getPOSList(), 
															String.valueOf(Qty) , 
															String.valueOf(Price), 
															String.valueOf(TotalAmount), 
															String.valueOf(Discount));
								
							}catch(Exception e){
								
							}
							
						}
						Define_ListOrder();
//						carcare_pay_adapter.add(new CarCare_List_Payment(false,getItemName(), 1, Double.valueOf(getItemPrice()), 0, Double.valueOf(getItemPrice()),getItemPriceOID()));
					
						
					}catch(Exception e){
//						 Toast.makeText(getApplicationContext(),
//									"ERROR: " + Data_user[position][8].toString(), Toast.LENGTH_SHORT).show();
					}
				}
			});
	        
		}else{//ItemPrice
			strarrDataItem = itemprice.SelectAll_ItemPrice(getItemGroupOID(),"","","");   
		    LV_ItemGroup.setAdapter(new CarCare_Adapter_ItemPrice(this,strarrDataItem));
	        // OnClick
	        LV_ItemGroup.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {
					try{
						setItemPriceOID	(strarrDataItem[position][0].toString());
						setCarTypeOID	(strarrDataItem[position][1].toString()); 
						setCarSizeOID	(strarrDataItem[position][2].toString()); 
						setCarGroupOID	(strarrDataItem[position][3].toString()); 
						setItemOID		(strarrDataItem[position][4].toString()); 
						setItemPrice	(strarrDataItem[position][5].toString());
						
						setCarGroupName	(strarrDataItem[position][6].toString()); 
						setCarSizeName	(strarrDataItem[position][7].toString()); 
						setCarTypeName	(strarrDataItem[position][8].toString()); 
						setItemCode		(strarrDataItem[position][9].toString()); 
						setItemName		(strarrDataItem[position][10].toString()); 
//						tv_item.setText("> "+getItemName());
						if(!Checkitem(getItemPriceOID())){
							try{
								servicepos.InsertServicePOSLists(DateTime.getDateNowymd()+DateTime.getTimeNowss(), 
						    			getPOSDocOID(), getItemPriceOID(), "1", getItemPrice(), getItemPrice(), 
						    			"0", "0", "", "", "", 
						    			"", "", "", "", getItemName(), 
						    			getFFUserOID(), DateTime.getDateTimeNow(), DateTime.getDateTimeNow(), "0", "0");
							}catch(Exception e){
								
							}
						}else{
							try{
								int Qty = Integer.valueOf(getListQty())+1;
								
								double Price = Double.valueOf(Number.unCommaReturnDouble(getListPrice()));
								double Discount = Double.valueOf(Number.unCommaReturnDouble(getListDiscount()));
								double TotalAmount = Double.valueOf(Number.unCommaReturnDouble(getListTotal()));
								
								TotalAmount = (Price*Qty)-Discount;
										
								servicepos.UpdateItemServicePOSLists( getPOSList(), 
															String.valueOf(Qty) , 
															String.valueOf(Price), 
															String.valueOf(TotalAmount), 
															String.valueOf(Discount));
								
							}catch(Exception e){
								
							}
							
						}
						Define_ListOrder();
//						carcare_pay_adapter.add(new CarCare_List_Payment(false,getItemName(), 1, Double.valueOf(getItemPrice()), 0, Double.valueOf(getItemPrice()),getItemPriceOID()));
					
						
					}catch(Exception e){
//						 Toast.makeText(getApplicationContext(),
//									"ERROR: " + Data_user[position][8].toString(), Toast.LENGTH_SHORT).show();
					}
				}
			});
//			strarrDataItem = item.SelectAll_Item(getItemGroupOID(),"");   
//			LV_ItemGroup.setAdapter(new CarCare_Adapter_Item(this,strarrDataItem));
//	        // OnClick
//	        LV_ItemGroup.setOnItemClickListener(new OnItemClickListener() {
//				public void onItemClick(AdapterView<?> parent, View v,
//					int position, long id) {
//					try{
//						setItemOID		(strarrDataItem[position][0].toString()); 
//						setItemPrice	(strarrDataItem[position][48].toString()); 
//		
//						setItemName		(strarrDataItem[position][2].toString()); 
////						tv_item.setText("> "+getItemName());
//						
//						if(!Checkitem(getItemOID())){
//							try{
//								servicepos.InsertServicePOSLists(DateTime.getDateNowymd()+DateTime.getTimeNowss(), 
//						    			getPOSDocOID(), getItemOID(), "1", getItemPrice(), getItemPrice(), 
//						    			"0", "0", "", "", "", 
//						    			"", "", "", "", getItemName(), 
//						    			getFFUserOID(), DateTime.getDateTimeNow(), DateTime.getDateTimeNow(), "0", "0");
//							}catch(Exception e){
//								
//							}
//						}else{
//							try{
//								int Qty = Integer.valueOf(getListQty())+1;
//								
//								double Price = Double.valueOf(Number.unCommaReturnDouble(getListPrice()));
//								double Discount = Double.valueOf(Number.unCommaReturnDouble(getListDiscount()));
//								double TotalAmount = Double.valueOf(Number.unCommaReturnDouble(getListTotal()));
//								
//								TotalAmount = (Price*Qty)-Discount;
//										
//								servicepos.UpdateItemServicePOSLists( getPOSList(), 
//															String.valueOf(Qty) , 
//															String.valueOf(Price), 
//															String.valueOf(TotalAmount), 
//															String.valueOf(Discount));
//								
//							}catch(Exception e){
//								
//							}
//						}
//						Define_ListOrder();
////						carcare_pay_adapter.add(new CarCare_List_Payment(false,getItemName(), 1, Double.valueOf(getItemPrice()), 0, Double.valueOf(getItemPrice()),getItemPriceOID()));
//					
//						
//					}catch(Exception e){
////						 Toast.makeText(getApplicationContext(),
////									"ERROR: " + Data_user[position][8].toString(), Toast.LENGTH_SHORT).show();
//					}
//				}
//			});
		} 
    
	}

	
	 private OnClickListener ButtonCheckall = new OnClickListener() {
	        public void onClick(View v) {
	        	try{
	        		strarrListPOS = servicepos.SelectAll_ServicePOSList(getPOSDocOID(),"0","");
	    			int length = strarrListPOS.length;	
	    			for(int i=0;i<length;i++){
	    				servicepos.UpdateStatusServicePOSLists(	strarrListPOS[i][0],
	    													"1", 
	    													"0");
	    			}
	    		}catch(Exception e){
	    		}
	        	Define_ListOrder();

	        }
	 }; 
	 private OnClickListener ButtonUnmark = new OnClickListener() {
	        public void onClick(View v) {
//	        	carcare_pay_adapter.clear();
	        	try{
	        		strarrListPOS = servicepos.SelectAll_ServicePOSList(getPOSDocOID(),"0","");
	    			int length = strarrListPOS.length;	
	    			for(int i=0;i<length;i++){
	    				servicepos.UpdateStatusServicePOSLists(	strarrListPOS[i][0],
	    													"0", 
	    													"0");
	    			}
	    		}catch(Exception e){
	    		}
	        	Define_ListOrder();
	        	
	        }
	 };
	 private OnClickListener ButtonClear = new OnClickListener() {
	        public void onClick(View v) {
//	        	carcare_pay_adapter.clear();
	        	try{
	        		strarrListPOS = servicepos.SelectAll_ServicePOSList(getPOSDocOID(),"0","");
	    			int length = strarrListPOS.length;	
	    			for(int i=0;i<length;i++){
	    				servicepos.UpdateStatusServicePOSLists(	strarrListPOS[i][0],
	    													"0", 
	    													"1");
	    			}
	    		}catch(Exception e){
	    		}
	        	setService_NetTotalAmount("0.00");
	        	Define_ListOrder();
	        	
	        }
	 };
	 
	 private OnClickListener Buttondelete = new OnClickListener() {
	        public void onClick(View v) {
	        	
	        	try{
	        		strarrListPOS = servicepos.SelectAll_ServicePOSList(getPOSDocOID(),"0","");
		        	int length = strarrListPOS.length;
		        	for(int i=0;i<length;i++){
		        		if(carcare_pay_adapter.getItem(i).isSelected()){
		        			servicepos.UpdateStatusServicePOSLists(	strarrListPOS[i][0],
																"0", 
																"1");
		        		}
		        	}
	        	}catch(Exception e){
	        		
	        	}
	        	
	        	Define_ListOrder();
	        	
	        }
	 };
	 
	 private OnClickListener ButtonSave = new OnClickListener() {
	        public void onClick(View v) {
	        	servicepos.UpdateServicePOSstatusUpdateByDoc(	
	        										getPOSDocOID(), 
	        										"0", 
	        										"0");
	        	try {
					String data[][] = servicepos.SelectAll_ServicePOS("", getPOSDocOID(), "");
					int time = DateTime.convertTime(data[0][5])+Integer.valueOf(getDuration());
//					Toast.makeText(getApplicationContext(),"time1 : " + time, Toast.LENGTH_SHORT).show();
//					Toast.makeText(getApplicationContext(),"time2 : " + settime(time), Toast.LENGTH_SHORT).show();
					setDuration(settime(time));
				} catch (Exception e) {
					// TODO Auto-generated catch block
					setDuration("");
				}
	        	servicepos.UpdatePriceAndDuration(	getPOSDocOID(), 
	        										getDuration(), 
	        										getService_total(), 
	        										getServicedbDiscount(), 
	        										getService_ReceiveMoney(), 
	        										getService_NetTotalAmount(), 
	        										"0");
	        	  finish();
//	        	onBackPressed();
	        }
	 };
	 
	 private String settime(int time){
		 String HH1,mm1;
		 int HH = time/60;
		 int mm = (time%60);
		 if(HH<10){
			HH1="0"+String.valueOf(HH);
		 }else{
			HH1=String.valueOf(HH);
		 }
		 if(mm<10){
			mm1="0"+String.valueOf(mm);
		 }else{
			mm1=String.valueOf(mm);
		 }
		 return HH1+":"+mm1;
	 }
	 
	  //------------------------ Exit ------------------------------------------//
	  	
	    @Override
		public void onBackPressed() {
		  
		        new AlertDialog.Builder(this)
		        .setTitle("บันทึก")
		        .setIcon(R.drawable.erroralert)
		        .setMessage("ต้องบันทึกการทำรายการ")
		                .setNegativeButton(android.R.string.yes, new DialogInterface.OnClickListener() {

		                    public void onClick(DialogInterface arg0, int arg1) {
		                        finish();
		                    }
		                })
		                .setPositiveButton(android.R.string.no, new DialogInterface.OnClickListener() {

		                    public void onClick(DialogInterface arg0, int arg1) {
		                    	
		                    }
		                }).create().show();
		} 
		
	 
	 	//Get Set
	 
		public String getItemGroupMainOID() {
			return ItemGroupMainOID;
		}

		public void setItemGroupMainOID(String itemGroupMainOID) {
			ItemGroupMainOID = itemGroupMainOID;
		}

		public String getItemGroupMainCode() {
			return ItemGroupMainCode;
		}

		public void setItemGroupMainCode(String itemGroupMainCode) {
			ItemGroupMainCode = itemGroupMainCode;
		}

		public String getItemGroupMainDesc() {
			return ItemGroupMainDesc;
		}

		public void setItemGroupMainDesc(String itemGroupMainDesc) {
			ItemGroupMainDesc = itemGroupMainDesc;
		}

		public String getItemGroupOID() {
			return ItemGroupOID;
		}

		public void setItemGroupOID(String itemGroupOID) {
			ItemGroupOID = itemGroupOID;
		}

		public String getItemGroupCode() {
			return ItemGroupCode;
		}

		public void setItemGroupCode(String itemGroupCode) {
			ItemGroupCode = itemGroupCode;
		}

		public String getItemGroupDesc() {
			return ItemGroupDesc;
		}

		public void setItemGroupDesc(String itemGroupDesc) {
			ItemGroupDesc = itemGroupDesc;
		}

		public String getItemPriceOID() {
			return ItemPriceOID;
		}

		public void setItemPriceOID(String itemPriceOID) {
			ItemPriceOID = itemPriceOID;
		}

		public String getCarTypeOID() {
			return CarTypeOID;
		}

		public void setCarTypeOID(String carTypeOID) {
			CarTypeOID = carTypeOID;
		}

		public String getCarSizeOID() {
			return CarSizeOID;
		}

		public void setCarSizeOID(String carSizeOID) {
			CarSizeOID = carSizeOID;
		}

		public String getCarGroupOID() {
			return CarGroupOID;
		}

		public void setCarGroupOID(String carGroupOID) {
			CarGroupOID = carGroupOID;
		}

		public String getItemOID() {
			return ItemOID;
		}

		public void setItemOID(String itemOID) {
			ItemOID = itemOID;
		}

		public String getItemPrice() {
			return ItemPrice;
		}

		public void setItemPrice(String itemPrice) {
			ItemPrice = itemPrice;
		}

		public String getCarGroupName() {
			return CarGroupName;
		}

		public void setCarGroupName(String carGroupName) {
			CarGroupName = carGroupName;
		}

		public String getCarSizeName() {
			return CarSizeName;
		}

		public void setCarSizeName(String carSizeName) {
			CarSizeName = carSizeName;
		}

		public String getCarTypeName() {
			return CarTypeName;
		}

		public void setCarTypeName(String carTypeName) {
			CarTypeName = carTypeName;
		}

		public String getItemCode() {
			return ItemCode;
		}

		public void setItemCode(String itemCode) {
			ItemCode = itemCode;
		}

		public String getItemName() {
			return ItemName;
		}

		public void setItemName(String itemName) {
			ItemName = itemName;
		}

		public String getFFUserOID() {
			return FFUserOID;
		}

		public void setFFUserOID(String fFUserOID) {
			FFUserOID = fFUserOID;
		}

		public String getArCode() {
			return ArCode;
		}

		public void setArCode(String arCode) {
			ArCode = arCode;
		}

		public String getCarSize() {
			return CarSize;
		}

		public void setCarSize(String carSize) {
			CarSize = carSize;
		}

		public String getPOSDocOID() {
			return POSDocOID;
		}

		public void setPOSDocOID(String pOSDocOID) {
			POSDocOID = pOSDocOID;
		}

		public String getPOSList() {
			return POSList;
		}

		public void setPOSList(String pOSList) {
			POSList = pOSList;
		}

		public String getListQty() {
			return ListQty;
		}

		public void setListQty(String listQty) {
			ListQty = listQty;
		}

		public String getListPrice() {
			return ListPrice;
		}

		public void setListPrice(String listPrice) {
			ListPrice = listPrice;
		}

		public String getListTotal() {
			return ListTotal;
		}

		public void setListTotal(String listTotal) {
			ListTotal = listTotal;
		}

		public String getListDiscount() {
			return ListDiscount;
		}

		public void setListDiscount(String listDiscount) {
			ListDiscount = listDiscount;
		}

		public String getServicedbDiscount() {
			return ServicedbDiscount;
		}

		public void setServicedbDiscount(String servicedbDiscount) {
			ServicedbDiscount = servicedbDiscount;
		}

		public String getService_total() {
			return Service_total;
		}

		public void setService_total(String service_total) {
			Service_total = service_total;
		}

		public String getService_ReceiveMoney() {
			return Service_ReceiveMoney;
		}

		public void setService_ReceiveMoney(String service_ReceiveMoney) {
			Service_ReceiveMoney = service_ReceiveMoney;
		}

		public String getService_NetTotalAmount() {
			return Service_NetTotalAmount;
		}

		public void setService_NetTotalAmount(String service_NetTotalAmount) {
			Service_NetTotalAmount = service_NetTotalAmount;
		}

		public String getDuration() {
			return Duration;
		}

		public void setDuration(String duration) {
			Duration = duration;
		}

		public String getPercent() {
			return percent;
		}

		public void setPercent(String percent) {
			this.percent = percent;
		}

		public String getDEFINECARSIZE() {
			return DEFINECARSIZE;
		}

		public void setDEFINECARSIZE(String dEFINECARSIZE) {
			DEFINECARSIZE = dEFINECARSIZE;
		}

		public String getDEFINECARGROUP() {
			return DEFINECARGROUP;
		}

		public void setDEFINECARGROUP(String dEFINECARGROUP) {
			DEFINECARGROUP = dEFINECARGROUP;
		}
		

}
